﻿using Rsd.Dudu;
using Rsd.Dudu.BizModel;
using Rsd.Dudu.Core;
using Rsd.Dudu.Log;
using Rsd.Dudu.Web;
using Rsd.Dudu.Web.Core;
using Rsd.Dudu.Web.MVC;

using Rsd.Faith.Core;
using Rsd.Faith.Models;
using Rsd.Faith.Sys.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

namespace Rsd.Faith.Web.Controllers
{
  
    /// <summary>
    /// 
    /// </summary>
    public class VerificationController : Rsd.Faith.Core.FaithController
    {
        /// <summary>
        /// 
        /// </summary>
        [WebMethod(EnableSession = false)]
        [PramaRule("token", "会话标识[token]", 3, 50,false,"")]
        [ApiService("会话验证",false)]
        public object CheckSession(string token)
        { 
            bool flag = true;
            string msg = "";
           
            JsonObject json = new JsonObject();

           
            if (flag)
            {
                flag = base.CheckAccessToken(token);
            }
            if (flag)
            { 
                SysUsersModel obj = base.GetSessionData<SysUsersModel>();

                json.Add("token", base.ResetSession());
                json.Add("XingMing", obj.XingMing);
            }
            if (flag == false)
            {
                msg = "会话失效，请重新登陆"; 
                this.GetService<ILogService>().Info(string.Format("会话({0})失效，请重新登陆。", string.IsNullOrEmpty(token) ? this.GetAccessToken() : token), "CheckSession");
            }
            return new DataResult(flag, msg, json);
            
               
        }
        /// <summary>
        /// 
        /// </summary>
        [WebMethod(EnableSession = false)]
        [PramaRule("userid", "用户名", 3, 15, true)]
        [PramaRule("pwd", "密码", 6, 18, true)]
        [ApiService("登录",false)]
        public object Login([Service]IEncryptService enService, [Service]IUserService userService,string userid,string pwd)
        {
            bool flag = true;
            string msg = "";
             
            JsonObject json = new JsonObject();
            
             
            var _new_pwd = enService.MD5(pwd).ToLower();
            //this.GetService<ILogService>().Debug(pwd);
             
            if (userService == null)
            {
                ErrorCodesType.W0002.SubmitError("IUserService");
                return null;
            }
            var  user = userService.GetUserObject(userid);
            if (user == null)
            {
                ErrorCodesType.W01001.SubmitError(userid);
                return null;
            }
            //验证
            flag = userService.ValidateUserNameAndPassword(userid, _new_pwd);

            if (flag)
            { 
                string key = base.SignIn(userid, user );
                json.Add("token", key);
                json.Add("XingMing", user.XingMing);

                msg = "验证通过。";
            }
            else
            {
                ErrorCodesType.W01002.SubmitError(this.Context.Request.GetClientIP(), userid, userid);
            }
            
            return new DataResult(flag, msg, json);
        }

        /// <summary>
        /// 
        /// </summary>
        [WebMethod(EnableSession = false)]
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = false, XmlSerializeString = false)]
        [ApiService("退出")]
        public object LogOut()
        {
            object user = null;
            base.SignOut();
            return new DataResult(true, "安全退出系统。", user);

        }
    }
}
